package edu.ccut.saturn.studio.server.model;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.w3c.dom.Document;
import org.w3c.dom.Node;

import edu.ccut.saturn.studio.core.exception.XMLException;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnEclipsePathUtil;
import edu.ccut.saturn.studio.core.util.freemaker.FreemarkerUtils;
import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;

public class SaturnServerModel {
	private static final String SATURN_SERVER_FTL = "template/saturn.ftl";

	public static Map<String, SaturnServer> getSaturnServers() {
		Document doc = getDocument();

		Map<String, SaturnServer> saturnServers = new HashMap<String, SaturnServer>();

		Node root = doc.getDocumentElement();
		
		List<Node> configNodes = SaturnW3CUtil.getChildrenByName(root, "config");

		if (configNodes != null) {
			for (Node configNode : configNodes) {
				SaturnServer server = new SaturnServer();
				String name = SaturnW3CUtil
						.getChildNodeValue(configNode, "name");
				String classType = SaturnW3CUtil.getChildNodeValue(configNode,
						"server");
				String serverPath = SaturnW3CUtil.getChildNodeValue(configNode,
						"server-path");
				String saturnPath = SaturnW3CUtil.getChildNodeValue(configNode,
						"saturn-path");
				String port = SaturnW3CUtil
						.getChildNodeValue(configNode, "port");

				server.setName(name);
				server.setDepolyPath(saturnPath);
				server.setServerPath(serverPath);
				server.setClassType(classType);
				server.setPort(port);

				saturnServers.put(name, server);
			}
		}

		return saturnServers;
	}

	public static void saveSaturnServers(Map<String, SaturnServer> saturnServers) {
		Document doc = getDocument();
		SaturnW3CUtil.clearDocument(doc);

		Node root = doc.getDocumentElement();

		if (!saturnServers.isEmpty() ) {
			for (String key : saturnServers.keySet()) {
				SaturnServer saturnServer = saturnServers.get(key);

				Node node = SaturnW3CUtil.createChildNode(root, "config", "");

				SaturnW3CUtil.createChildNode(node, "name", saturnServer
						.getName());
				SaturnW3CUtil.createChildNode(node, "server", saturnServer
						.getClassType());
				SaturnW3CUtil.createChildNode(node, "server-path", saturnServer
						.getServerPath());
				SaturnW3CUtil.createChildNode(node, "saturn-path", saturnServer
						.getDepolyPath());
				SaturnW3CUtil.createChildNode(node, "port", saturnServer
						.getPort());

				SaturnW3CUtil.format(node);
				String serverXmlPath = SaturnEclipsePathUtil
						.getEclipseConfigurationDirectory()
						+ "/server.xml";
				try {
					SaturnW3CUtil.writeDocument(doc, serverXmlPath);
				} catch (XMLException e) {
					e.printStackTrace();
				}
			}
		} else {
			SaturnW3CUtil.removeChildren(root);
			
			String serverXmlPath = SaturnEclipsePathUtil
					.getEclipseConfigurationDirectory()
					+ "/server.xml";
			try {
				SaturnW3CUtil.writeDocument(doc, serverXmlPath);
			} catch (XMLException e) {
				e.printStackTrace();
			}
		}
	}

	private static Document getDocument() {
		String serverXmlPath = SaturnEclipsePathUtil
				.getEclipseConfigurationDirectory()
				+ "\\server.xml";
		InputStream input = null;

		try {
			File file = new File(serverXmlPath);
			if (!new File(serverXmlPath).exists()) {
				FreemarkerUtils.writeTemplateToFile(SATURN_SERVER_FTL, null,
						serverXmlPath);
			}
			
			input = new FileInputStream(file);
			return SaturnW3CUtil.getDocument(input);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (input != null) {
				try {
					input.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return null;
	}
}
